import storage from '@/utils/storage'
import router from '@/router/index'
import store from '@/store'
let lastTime = new Date().getTime()
let currentTime = new Date().getTime()
const timeOut = 30 * 60 * 1000 // 设置超时时间: 30分钟

window.onload = function() {
  window.document.onmousedown = function() {
    storage.setItem('lastTime', new Date().getTime())
  }
}

function checkTimeout() {
  currentTime = new Date().getTime() // 更新当前时间
  lastTime = storage.getItem('lastTime')
  if (currentTime - lastTime > timeOut) {
    // 判断是否超时
    console.log('超时未操作，退出登录')
    store.dispatch('user/LogOut').then(() => {
      storage.removeItem('lastTime')
      window.location.reload()
    })
  }
}

export default function() {
  /* 定时器 间隔60秒检测是否长时间未操作页面 */
  window.setInterval(checkTimeout, 60000)
}
